


// curl -X POST -d '{"api_name": "stock_basic", "token": "xxxxxxxx", "params": {"list_stauts":"L"}, "fields": "ts_code,name,area,industry,list_date"}' http://api.tushare.pro

use std::{io::Write, process::Command};

use cross_rs::IResult;


#[cfg(windows)]
const CMD: &'static str = "C:/PF/curl-8.5.0/curl.exe";
#[cfg(unix)]
const CMD: &'static str = "curl";

pub fn http_post(url: &str, json: &str) -> IResult<String> {
    let mut cmd_order = Command::new(CMD);

    cmd_order
        .arg("-X")
        .arg("POST")
        .arg("-d")
        .arg(json)
        .arg(url);

    let output = cmd_order.output()?;
    let mut buf = vec![];
    
    buf.write_all(&output.stdout)?;

    let text = String::from_utf8(buf)?;

    Ok(text)

}

#[test]
pub fn test_http_post() {
    use crate::disk::file_io;

    let url = "http://api.tushare.pro";
    let json = "{\"api_name\": \"stock_basic\", \"token\": \"64500155c945e660eed14724a90676e18d261f2932dcfcdf8138123d\", \"params\": {\"list_stauts\": \"L\"}, \"fields\": \"ts_code,name,area,industry,list_date\"}";
    match http_post(url, json) {
        Ok(v) => {
            #[cfg(windows)]
            let file_text = "I:/data/2.txt";
            #[cfg(unix)]
            let file_text = "/mnt/i/data/1.txt";
            let count_字符 = v.len();
            file_io::write_utf8(file_text, &v);
            println!("curl::http_post 成功！写入 {count_字符} 个字符, {file_text}");
        },
        Err(e) => {
            println!("curl::http_post 失败！{e:?}");
        },
    }
}